import cache from './../../utils/cache'
import STORE_KEY from './../key'
const state = {
    //账户一些基本信息
    token: cache.get(cache.key.ACCESS_TOKEN),
    user: cache.get(cache.key.USER_INFO),
    permissions: cache.get(cache.key.PERMISSION)
}

const mutations = {
    M_UPDATE_TOKEN(state, token) {
        state.token = token
        cache.set(cache.key.ACCESS_TOKEN, token)
    },
    //此处的user是对象，不是&ss=xx形似(这个形式是为了兼容易起云那边的缓存)
    M_UPDATE_USER(state, user) {
        state.user = user
        cache.set(cache.key.USER_INFO, user)
    },
    M_CLEAR_TOKEN(state) {
        state.token = null
        cache.remove(cache.key.ACCESS_TOKEN)
        state.user = null
        cache.remove(cache.key.USER_INFO)
        state.permissions = null
        cache.remove(cache.key.PERMISSION)
    },
    M_UPDATE_PREMISSION(state, permissions) {
        state.permissions = permissions
        cache.set(cache.key.PERMISSION, permissions)
    }
}

const actions = {
    A_LOGIN(store, payload) {
        const { commit } = store
        commit('M_UPDATE_TOKEN', payload)
        // commit('M_UPDATE_USER', payload)
    },
    A_CLEAR_TOKEN(store) {
        const { dispatch, commit } = store
        commit('M_CLEAR_TOKEN')
        dispatch(STORE_KEY.A_APP_CLEAR_TOKEN, null, { root: true })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
